#include "myhead.h"
#include "head.h"




//管理员登录
void admin_menu()
{
    
    int choice;
    do
    {
        CLEAR_SCREEN();
        MOVE_CURSOR(5,25);

        printf("请输入选项：\n");

        MOVE_CURSOR(8,26);
        SET_COLOR(COLOR_YELLOW,0);
        printf("1.存入书籍");

        MOVE_CURSOR(11,26);
        printf("2.删除书籍");

        MOVE_CURSOR(14,26);
        printf("3.显示全部书籍");

        MOVE_CURSOR(17,26);
        printf("4.退出");

        choice = getchoice(1,4,19,26);

        switch (choice)
        {
        case 1:
            DepositInBooks();
            break;
        
        case 2:
            DeleteBooks();
            break;

        case 3:
            ShowAllBooks();
            break;

        case 4:
            return;       
       
        }
    } while (1);
    
}

//存入书籍
void DepositInBooks()
{
    int num;
    int duplicate = 0;
    book b;

    CLEAR_SCREEN();
    MOVE_CURSOR(5,26);
    printf("存入书籍");
    SET_COLOR(COLOR_YELLOW,0);

    MOVE_CURSOR(8,26);
    printf("书号:");
    scanf("%d",&num);
    getchar();

    for(int i = 0; i<book_count;i++)
    {
        if (num == books[i].Number)
    {
        SET_COLOR(COLOR_ERROR,0);
        MOVE_CURSOR(9,26);
        printf("\n已有书号");
        getchar();
        duplicate = 1;
        break;
    }
   
    }

    
    if (!duplicate)
    {
        b.Number = num;

        SET_COLOR(COLOR_YELLOW,0);
        MOVE_CURSOR(11,26);
        printf("书名:");
        getinput(b.Title, sizeof(b.Title), 11, 31);
        MOVE_CURSOR(12,26);
        CLEAR_LINE();

        MOVE_CURSOR(14,26);
        printf("作者:");
        getinput(b.Author,sizeof(b.Author),14,31);
        MOVE_CURSOR(15,26);
        CLEAR_LINE();
       
        b.Status = 0;
        books[book_count] = b;
        book_count++;
        MOVE_CURSOR(17,26);
        SET_COLOR(COLOR_SUCCESS,0);
        printf("书籍添加成功");
        MOVE_CURSOR(18,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
    }
    RESET_COLOR();
    getchar();
}

//删除书籍
void DeleteBooks()
{
    int num;
    int found = 0;
  
    CLEAR_SCREEN();
    MOVE_CURSOR(5,26);
    printf("删除书籍");
    SET_COLOR(COLOR_YELLOW,0);

    MOVE_CURSOR(8,26);
    printf("书号");
    scanf("%d",&num);
    getchar();

    for (int i = 0; i < book_count; i++)
    {
        if (num == books[i].Number)
        {
            found = 1;
            for (int j = i; j < book_count-1; j++)
            {
                books[j] = books[j+1];
            }
            book_count--;
            MOVE_CURSOR(9,26);
            SET_COLOR(COLOR_SUCCESS,0);
            printf("已删除");
            break;
        }     
    }
    if(!found)
    {
        SET_COLOR(COLOR_ERROR,0);
        printf("找不到");   
    }
    MOVE_CURSOR(10,26);
    SET_COLOR(COLOR_TITLE,0);
    Enter();
getchar();
}

//显示所有书籍
void ShowAllBooks()
{
    CLEAR_SCREEN();

    if (book_count == 0)
    {
        MOVE_CURSOR(5,26);
        SET_COLOR(COLOR_ERROR,0);
        printf("没有图书记录");
        MOVE_CURSOR(8,26);
        SET_COLOR(COLOR_TITLE,0);
        Enter();
        getchar();
        return;
    }
    
    MOVE_CURSOR(5,26);
    printf("所有书籍信息");
    SET_COLOR(COLOR_YELLOW,0);
    MOVE_CURSOR(8,26);
    printf("%-10s %-20s %-17s %-10s", "书号", "书名", "作者", "状态");

    for (int i = 0; i < book_count; i++)
    {
        MOVE_CURSOR(9+i,26);
        printf("%-10d %-20s %-10s %-10s", 
            books[i].Number,books[i].Title,books[i].Author,books[i].Status ?"已借阅":"未借阅");       
            MOVE_CURSOR(11+i,26);
    }
    SET_COLOR(COLOR_TITLE,0);
    Enter();
    getchar();
}